#note:gcc -O -fpic -shared -o  dl.so  pr1.c
#-fpic :-f后面跟一些编译选项，PIC是其中一种，表示生成位置无关代码（Position Independent Code）
#ar  -rsv  libpart.a  part1.o part2.o
#

CFG_DEBUG ?= 1


common_src :=  $(wildcard ./*.c)
common_src_nodir := $(notdir $(common_src))
common_objs := $(common_src:%.c=%.o)
common_objs_objdir := $(common_src_nodir:%.c=objs/%.o)


src := $(wildcard ./*.c)
src_nodir := $(notdir $(src))


objs := $(src:%.c=%.o)
objs_objdir := $(src_nodir:%.c=objs/%.o)


CFLAGS +=  -c -fPIC
LDFAGS := -lpthread -g


ifeq ($(strip $(CFG_DEBUG)),1)
	CFLAGS += -DDEBUG -g
endif


share_mem_static_lib = libsharemem.a
share_mem_dynamic_lib = libsharemem.so
.PHONY: all clean $(share_mem_synamic_lib) $(share_mem_dynamic_lib)


all:$(share_mem_dynamic_lib) $(share_mem_dynamic_lib)

$(share_mem_dynamic_lib):$(objs)
	@echo "[ LD $(notdir objs) ==> $@ ]"
	$(Q)$(CC) -o $@ -shared -fPIC $(common_objs_objdir) 

$(share_mem_static_lib):$(objs)
	@echo "[ LD $(notdir objs) ==> $@ ]"
	$(Q) $(AR) rcu $@   $(common_objs_objdir) 

$(filter %.o,$(objs)):%.o:%.c
	@echo  "[ CC $(notdir $^) ]"
	$(Q)$(CC) $(CFLAGS) $< -o objs/$(notdir $@)

#clean the object file
clean:
	-rm -rf *.o objs/*.o  $(share_mem_static_lib) $(share_mem_dynamic_lib)
